package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.Message;
import com.symone.webcms.model.base.ResultMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.plugins.Page;
import com.symone.webcms.service.NotificationService;
import com.symone.webcms.model.Notification;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *   @description : 提醒管理
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-03-19
 */
@RestController
@Api(value="/notification", description="提醒管理")
@RequestMapping("api/notification")
public class NotificationController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(NotificationController.class);

    @Autowired
    public NotificationService notificationService;

    /**
     * @description : 获取Notification分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-19
     */
    @RequiresPermissions("cms:notification:index")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ApiOperation(value="获取提醒分页列表", notes="获取提醒分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageSize",value="页大小",required=true,defaultValue = "10"),
            @ApiImplicitParam(name="pageNum",value="页数",required=true,defaultValue = "1")
    })
    public ResultMessage page(Integer pageSize,Integer pageNum) {
        Wrapper<Notification> ew = new EntityWrapper<Notification>();
        ew.where("status=1");
        ew.and().eq("user_id",getSessionUser().getId());
        ew.orderBy("create_time",false);

        //logger.debug("SQL:"+ew.originalSql());

        Page<Notification> page = new Page<Notification>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        page = notificationService.findPage(page, ew);
        notificationService.clearAllByUid(getSessionUser().getId());
        return ResultMessage.success("获取提醒的分页列表",page.getRecords(),page.getTotal());
    }

    /**
     * @description : 获取所有提醒记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-19
     */
    @RequiresPermissions("cms:notification:index")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value="获取所有提醒信息", notes="获取所有提醒信息")
    public ResultMessage list() {
        return ResultMessage.success("获取所有提醒信息",notificationService.findAll());
    }

    /**
     * @description : 通过id获取Notification
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-19
     */
    @RequiresPermissions("cms:notification:get")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ApiOperation(value="获取提醒信息", notes="获取提醒信息")
    @ApiImplicitParam(name="id",value="提醒ID",required=true)
    public ResultMessage get(Long id) {
        return ResultMessage.success("获取提醒信息成功!",notificationService.selectById(id));
    }

    /**
     * @description : 添加Notification
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-19
     */
    @RequiresPermissions("cms:notification:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value="添加提醒信息", notes="添加提醒信息")
    @ApiImplicitParam(name = "notification", value = "提醒信息", required = true )
    public ResultMessage add(Notification notification){
        notificationService.insert(notification);
        return ResultMessage.success("添加提醒成功!");
    }

    /**
     * @description : 通过id逻辑删除提醒
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-19
     */
    @RequiresPermissions("cms:notification:remove_flag")
    @RequestMapping(value = "/removeFlag", method = RequestMethod.PUT)
    @ApiOperation(value="逻辑删除提醒", notes="逻辑删除提醒")
    @ApiImplicitParam(name = "id", value = "提醒Id", required = true )
    public ResultMessage removeFlag(Long id){
        if(notificationService.deleteById(id)) {
            return ResultMessage.success("逻辑提醒删除成功！");
        } else {
            return ResultMessage.fail("逻辑提醒删除失败！");
        }
    }

    /**
     * @description : 通过id逻辑删除提醒
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-19
     */
    @RequiresPermissions("cms:notification:remove")
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    @ApiOperation(value="物理删除提醒", notes="物理删除提醒")
    @ApiImplicitParam(name = "id", value = "提醒Id", required = true )
    public ResultMessage remove(long id){
        if(notificationService.deleteById(id)) {
            return ResultMessage.success("物理删除提醒成功！");
        } else {
            return ResultMessage.fail("物理删除提醒失败！");
        }
    }

    /**
     * @description : 更新提醒信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-19
     */
    @RequiresPermissions("cms:notification:edit")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    @ApiOperation(value="更新提醒信息", notes="更新提醒信息")
    @ApiImplicitParam(name = "notification", value = "提醒信息", required = true )
    public ResultMessage update(Notification notification) {
        if(notificationService.updateById(notification)) {
            return ResultMessage.success("提醒信息更新成功!");
        }else{
            return ResultMessage.fail("提醒信息更新失败!");
        }
    }

    /**
    * @description : 清除提醒记录
    * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-19
    */
    @RequiresPermissions("cms:notification:clear")
    @RequestMapping(value = "/clear", method = RequestMethod.DELETE)
    @ApiOperation(value="清除提醒记录", notes="清除已被逻辑删除的提醒记录")
    public ResultMessage clear(){
        notificationService.clearAllByUid(getSessionUser().getId());
        return ResultMessage.success("我的提醒被清理！");
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    public ResultMessage getNotificationCount(){
        int count = notificationService.selectCount(
                new EntityWrapper<Notification>()
                        .eq("user_id",getSessionUser().getId())
                        .eq("is_read",0)
        );
        return ResultMessage.success("获取当前用户的新消息数",count);
    }

}
